package Action.Manager;

import Action.BackgroundThread.RecommendedbooksThread;
import Action.Sys.Setting;
import Bean.BookInfoEntity;
import Bean.LoanBookLogRecord;
import Bean.Log.OrderLog;
import Bean.UserinfoEntity;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;

import java.util.List;

public class AltOrder extends ActionSupport {
    int modify_key;//订单对象，用户对象，图书对象
    Object[] modify_ob;
    List<OrderLog> orderLogs;//订单日志


    {
        RecommendedbooksThread.sys_busy=true;
    }
    public String OrderDetail(){//本逻辑供用户端复用,不可对此方法调用进行登录拦截
        Session session=HibernateTools.getSession();
        LoanBookLogRecord loanBookLogRecord=session.get(LoanBookLogRecord.class, modify_key);
        if (loanBookLogRecord==null)
        {
            addActionError("订单不存在");
            HibernateTools.commit(session);
            return ERROR;
        }
        UserinfoEntity userinfoEntity=session.get(UserinfoEntity.class,loanBookLogRecord.getUserId());
        if (userinfoEntity==null){
            userinfoEntity=new UserinfoEntity();
            userinfoEntity.setUsername("已删除的用户ID："+loanBookLogRecord.getUserId());
        }

        BookInfoEntity bookInfoEntity=session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
        if (bookInfoEntity==null){
            bookInfoEntity=new BookInfoEntity();
            bookInfoEntity.setBook_name("已删除的图书ID："+loanBookLogRecord.getBookId());
        }
        modify_ob= new Object[]{loanBookLogRecord,userinfoEntity,bookInfoEntity};

        buildOrderLog();
        HibernateTools.commit(session);
        return SUCCESS;
    }

    private void buildOrderLog() {
        Session session=HibernateTools.getSession();
        orderLogs=session.createQuery("from OrderLog where orderid =?").setParameter(0,modify_key).getResultList();
//        HibernateTools.commit(session);
    }

    public String order_cancel(){
        clearErrorsAndMessages();
        if (modify_key<0) {
            return INPUT;
        }
        Session session=HibernateTools.getSession();
        LoanBookLogRecord loanBookLogRecord=session.get(LoanBookLogRecord.class, modify_key);
        if (loanBookLogRecord==null){
            addActionError("订单不存在");
            OrderDetail();
            return ERROR;
        }
        if (!loanBookLogRecord.getState().equals(Setting.LoanBook_state_reservation)){
            addActionError("不可取消");
            OrderDetail();
            return ERROR;
        }
        //正确逻辑
        loanBookLogRecord.setState(Setting.LoanBook_state_cancel);
        session.update(loanBookLogRecord);
        BookInfoEntity bookInfoEntity=session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
        bookInfoEntity.setUsed(bookInfoEntity.getUsed()-1);
        session.update(bookInfoEntity);

        OrderLog.addLog(loanBookLogRecord.getId(), "管理员取消订单");
        HibernateTools.commit(session);
        return OrderDetail();
    }
    public String order_loan(){// 借出
        clearErrorsAndMessages();

        if (modify_key<0) {
            return INPUT;
        }
        Session session=HibernateTools.getSession();
        LoanBookLogRecord loanBookLogRecord=session.get(LoanBookLogRecord.class, modify_key);
        if (loanBookLogRecord==null){
            addActionError("订单不存在");
            OrderDetail();
            return ERROR;
        }
        if (!loanBookLogRecord.getState().equals(Setting.LoanBook_state_reservation)){
            addActionError("订单状态异常");
            OrderDetail();
            return ERROR;
        }
        //正确逻辑
        loanBookLogRecord.setState(Setting.LoanBook_state_need_Return);
        session.update(loanBookLogRecord);

        OrderLog.addLog(loanBookLogRecord.getId(),"管理员借出");
        HibernateTools.commit(session);
        return OrderDetail();
    }
    public String order_return(){//归还
        clearErrorsAndMessages();
        if (modify_key<0) {
            return INPUT;
        }
        Session session=HibernateTools.getSession();
        LoanBookLogRecord loanBookLogRecord=session.get(LoanBookLogRecord.class, modify_key);
        if (loanBookLogRecord==null){
            addActionError("订单不存在");
            OrderDetail();
            return ERROR;
        }
        if (!loanBookLogRecord.getState().equals(Setting.LoanBook_state_need_Return)&&!loanBookLogRecord.getState().equals(Setting.LoanBook_state_need_TimeOut)){
            addActionError("不能归还");
            OrderDetail();
            return ERROR;
        }
        //正确逻辑
        loanBookLogRecord.setState(Setting.LoanBook_state_closed);
        session.update(loanBookLogRecord);
        BookInfoEntity bookInfoEntity=session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
        bookInfoEntity.setUsed(bookInfoEntity.getUsed()-1);
        session.update(bookInfoEntity);

        OrderLog.addLog(loanBookLogRecord.getId(), "归还书籍");
        HibernateTools.commit(session);
        addActionMessage("归还成功");
        return OrderDetail();
    }




    public Object[] getModify_ob() {
        return modify_ob;
    }

    public void setModify_ob(Object[] modify_ob) {
        this.modify_ob = modify_ob;
    }

    public int getModify_key() {
        return modify_key;
    }

    public void setModify_key(int modify_key) {
        this.modify_key = modify_key;
    }

    public List<OrderLog> getOrderLogs() {
        return orderLogs;
    }

    public void setOrderLogs(List<OrderLog> orderLogs) {
        this.orderLogs = orderLogs;
    }
}
